package cn.chuanw.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.chuanw.wx.annotation.RequiresPermissionsDesc;
import cn.chuanw.core.util.RandomNumberUtil;
import cn.chuanw.core.util.ResponseUtil;
import cn.chuanw.db.dao.WHouseInfoMapper;
import cn.chuanw.db.domain.WHouseInfo;
import cn.chuanw.db.domain.WRentpayRecord;
import cn.chuanw.db.service.WRentpayRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/rentpay")
@Validated
public class WRentpayRecordController {
    private final Log logger = LogFactory.getLog(WRentpayRecordController.class);

    @Autowired
    private WRentpayRecordService wRentpayRecordService;
    @Autowired
    private WHouseInfoMapper wHouseInfoMapper;
    
    @RequiresPermissions("admin:rentpay:list")
    @RequiresPermissionsDesc(menu = {"公寓管理", "房屋房东交租信息"}, button = "查询")
    @GetMapping("/list")
    public Object list(String userId,
    	         	String houseId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {
        List<WRentpayRecord> wRentpayRecords = wRentpayRecordService.querySelective(userId,houseId,page, limit);
        return ResponseUtil.okList(wRentpayRecords);
    }


    @RequiresPermissions("admin:rentpay:add")
    @RequiresPermissionsDesc(menu = {"公寓管理", "房屋房东交租信息"}, button = "新增")
    @PostMapping("/add")
    public Object add(@RequestBody WRentpayRecord wRentpayRecord) {
    	wRentpayRecord.setId(RandomNumberUtil.getStringRandom(12));
    	wRentpayRecordService.addWHouseInfo(wRentpayRecord);
        return ResponseUtil.ok(wRentpayRecord);
    }
    @RequiresPermissions("admin:rentpay:addHouse")
    @RequiresPermissionsDesc(menu = {"公寓管理", "房屋房东交租信息"}, button = "交租记录")
    @PostMapping("/addHouseRentPay")
    public Object addHouse(@RequestBody WHouseInfo wHouseInfo) {
    	WRentpayRecord wRentpayRecord =new WRentpayRecord();
    	
    	//获得支付方式
    	int cycle =Integer.parseInt(wHouseInfo.getCycle());
    	wRentpayRecord.setHouseId(wHouseInfo.getId());
    	wRentpayRecord.setId(RandomNumberUtil.getStringRandom(12));
    	wRentpayRecord.setPayAmount(wHouseInfo.getRent());
    	wRentpayRecord.setUserId(wHouseInfo.getUserId());
    	wRentpayRecord.setPayTime(LocalDateTime.now());
    	wRentpayRecord.setId(RandomNumberUtil.getStringRandom(12));
    	wRentpayRecord.setPayStatus(0);//修改为已支付
    	wRentpayRecordService.addWHouseInfo(wRentpayRecord);

    	WHouseInfo wHouseInfos = new WHouseInfo();
    	if(cycle==4){
    		cycle=6;
    	}
    	wHouseInfos.setPlayTime(wHouseInfo.getPlayTime().plusMonths(cycle));
    	wHouseInfos.setPlayFlag(0);//修改为已支付
    	wHouseInfos.setId(wHouseInfo.getId());
    	wHouseInfoMapper.updateByPrimaryKeySelective(wHouseInfos);
        return ResponseUtil.ok(wRentpayRecord);
    }

    @RequiresPermissions("admin:rentpay:update")
    @RequiresPermissionsDesc(menu = {"公寓管理", "房屋房东交租信息"}, button = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody WRentpayRecord wRentpayRecord) {
    	wRentpayRecordService.updateById(wRentpayRecord);
        return ResponseUtil.ok(wRentpayRecord);
    }
}
